from django.shortcuts import render
from rest_framework import status
from rest_framework.generics import CreateAPIView, UpdateAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from user.models import UserModel
from user.serial import UserSerializer
from user.service import get_message
from rest_framework.permissions import IsAuthenticated

# Create your views here.

# 注册视图
class Register(CreateAPIView):
    queryset = UserModel.objects.all()
    serializer_class = UserSerializer

# 大模型多轮问答视图
class QwView(APIView):
    permission_classes = [IsAuthenticated]

    def get(self, request):
        message = request.query_params.get("message", None)
        if message == "" or message is None:
            return Response({"msg": "提问框为空"}, status=status.HTTP_400_BAD_REQUEST)
        data = get_message(message)
        return Response({"msg": "ok", "data": data})

# 用户展示视图
class UserRetrieveView(APIView):
    permission_classes = [IsAuthenticated]

    def get(self, request):
        user = request.user
        print(user.avatar)
        data = {
            "id": user.id,
            "username": user.username,
            "mobile": user.mobile,
            "email": user.email,
            "gender": user.gender,
            "gender_display": user.get_gender_display(),
            "age": user.age,
            "avatar": "http://127.0.0.1:8000" + user.avatar.url,
        }
        print(data)
        return Response({"msg": "ok", "data": data})

# 用户修改视图
class UserUpdateView(UpdateAPIView):
    permission_classes = [IsAuthenticated]
    queryset = UserModel
    serializer_class = UserSerializer
    lookup_field = "pk"
    lookup_url_kwarg = "pk"

# # 用户详情展示视图
# class UserDetailListView(ListAPIView):
#     queryset = UserDetail
#     serializer_class = UserDetailSerializer
#
# # 用户详情创建视图
# class UserDetailCreateView(CreateAPIView):
#     queryset = UserDetail
#     serializer_class = UserDetailSerializer
#
# # 用户详情修改视图
# class UserDetailUpdateView(UpdateAPIView):
#     queryset = UserDetail
#     serializer_class = UserDetailSerializer
#     lookup_field = "pk"
#     lookup_url_kwarg = "pk"
